package com.ruoyi.dept.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.dept.domain.BsInstrument;
import com.ruoyi.dept.domain.vo.BsDepartmentTreeVo;
import com.ruoyi.dept.mapper.BsInstrumentMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dept.domain.bo.BsDepartmentBo;
import com.ruoyi.dept.domain.vo.BsDepartmentVo;
import com.ruoyi.dept.domain.BsDepartment;
import com.ruoyi.dept.mapper.BsDepartmentMapper;
import com.ruoyi.dept.service.IBsDepartmentService;

import java.util.*;

/**
 * 科室Service业务层处理
 *
 * @author zyb
 * @date 2024-04-22
 */
@RequiredArgsConstructor
@Service
public class BsDepartmentServiceImpl implements IBsDepartmentService {

    private final BsDepartmentMapper baseMapper;

    private final BsInstrumentMapper bsInstrumentMapper;

    /**
     * 查询科室
     */
    @Override
    public BsDepartmentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询科室列表
     */
    @Override
    public TableDataInfo<BsDepartmentVo> queryPageList(BsDepartmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsDepartment> lqw = buildQueryWrapper(bo);
        Page<BsDepartmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询科室列表
     */
    @Override
    public List<BsDepartmentVo> queryList(BsDepartmentBo bo) {
        LambdaQueryWrapper<BsDepartment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsDepartment> buildQueryWrapper(BsDepartmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsDepartment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, BsDepartment::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BsDepartment::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增科室
     */
    @Override
    public Boolean insertByBo(BsDepartmentBo bo) {
        BsDepartment add = BeanUtil.toBean(bo, BsDepartment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }
    /**
     * 新增科室
     */
    @Override
    public BsDepartment insertBsDepartmentByBo(BsDepartmentBo bo) {
        BsDepartment add = BeanUtil.toBean(bo, BsDepartment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return add;
    }

    /**
     * 修改科室
     */
    @Override
    public Boolean updateByBo(BsDepartmentBo bo) {
        BsDepartment update = BeanUtil.toBean(bo, BsDepartment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsDepartment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除科室
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids){
            bsInstrumentMapper.delete(new LambdaQueryWrapper<BsInstrument>().eq(BsInstrument :: getDpmId,String.valueOf(id)));
        }

        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<BsDepartmentTreeVo> tree(BsDepartmentBo bo) {
        List<BsDepartmentTreeVo> treeVoList = new ArrayList<>();
        LambdaQueryWrapper<BsDepartment> lqw = buildQueryWrapper(bo);
        List<BsDepartment> bsDepartments = baseMapper.selectList(lqw);
        bsDepartments.forEach(t->{
            BsDepartmentTreeVo treeVo = new BsDepartmentTreeVo();
            treeVo.setId(t.getId());
            treeVo.setName(t.getName());
            List<BsDepartmentTreeVo> treeList = new ArrayList<>();
            List<BsInstrument> instrumentList = bsInstrumentMapper.selectList(new LambdaQueryWrapper<BsInstrument>().eq(BsInstrument::getDpmId, String.valueOf(t.getId())));
            if (CollectionUtils.isNotEmpty(instrumentList)){
                instrumentList.forEach(e -> {
                    BsDepartmentTreeVo tree = new BsDepartmentTreeVo();
                    tree.setId(e.getInstruId());
                    tree.setName(e.getInstruName());
                    tree.setCode(e.getInstruCode());
                    treeList.add(tree);
                });
                treeVo.setChildren(treeList);
            }
            treeVoList.add(treeVo);
        });
        return treeVoList;
    }
}
